package com.ourygo.fileselect.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ourygo.fileselect.R;
import com.ourygo.fileselect.bean.Files;
import com.ourygo.fileselect.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class FileListAdapter extends BaseQuickAdapter<Files, BaseViewHolder>
{
	private List<Files> data;
	private Context context;
	private boolean isMultiSelect;
	private ArrayList<String> selectList;
	
	public FileListAdapter(Context context,List<Files> data,boolean isMultiSelect){
		super(R.layout.item_file_list,data);
		this.data=data;
		this.context=context;
		this.isMultiSelect=isMultiSelect;
		if(isMultiSelect){
			selectList=new ArrayList<String>();
		}
	}
	
	public void removeSelect(String path){
		for(int i=0;i<selectList.size();i++){
			if(path.equals(selectList.get(i))){
				selectList.remove(i);
				notifyDataSetChanged();
				return;
			}
		}
	}
	
	public ArrayList<String> getSelectList() {
		return selectList;
	}


	@Override
	protected void convert(BaseViewHolder helper, Files item) {
		CheckBox cb_select=helper.getView(R.id.cb_select);
		ImageView iv_icon=helper.getView(R.id.iv_icon);
		if(isMultiSelect&&!item.isDirectory()){
			cb_select.setVisibility(View.VISIBLE);
			boolean b=false;
			for(String s:selectList){
				if(s.equals(item.getAbsolutePath())){
					b=true;
					break;
				}
			}
			cb_select.setChecked(b);

		/*	cb_select.setOnCheckedChangeListener(new OnCheckedChangeListener(){

					@Override
					public void onCheckedChanged(CompoundButton p1, boolean p2) {
						if (p2) {
							selectList.add(position);
						} else {
							for (int i=0;i < selectList.size();i++) {
								if(selectList.get(i)==position){
									selectList.remove(i);
								}
							}
						}
						// TODO: Implement this method
					}
				});*/
		}else{
			cb_select.setVisibility(View.GONE);
		}

		helper.setText(R.id.tv_name,item.getName());

		switch (item.getType()){
			case Files.FILE_TYPE_PATH:
				Utils.setImage(context,iv_icon,R.drawable.listfile_folder);
				break;
			case Files.FILE_TYPE_FILE:
				Utils.setImage(context,iv_icon,R.drawable.listfile_file);
				break;
			case Files.FILE_TYPE_CDB:
				Utils.setImage(context,iv_icon,R.drawable.listfile_cdb_iv);
				break;
			case Files.FILE_TYPE_CONF:
				Utils.setImage(context,iv_icon,R.drawable.listfile_conf);
				break;
			case Files.FILE_TYPE_CARD_PIC:
				Utils.setImage(context,iv_icon,R.drawable.listfile_pics_iv);
				break;
			case Files.FILE_TYPE_LUA:
				Utils.setImage(context,iv_icon,R.drawable.listfile_lua_iv);
				break;
			case Files.FILE_TYPE_ENDING:
				Utils.setImage(context,iv_icon,R.drawable.listfile_ending_iv);
				break;
			case Files.FILE_TYPE_ZIP:
				Utils.setImage(context,iv_icon,R.drawable.listfile_zip);
				break;
			case Files.FILE_TYPE_PIC:
				Utils.setImage(context,iv_icon,item.getAbsolutePath());
				break;
			case Files.FILE_TYPE_TXT:
				Utils.setImage(context,iv_icon,R.drawable.listfile_txt);
				break;
			case Files.FILE_TYPE_YDK:
				Utils.setImage(context,iv_icon,R.drawable.listfile_ydk_iv);
				break;
			case Files.FILE_TYPE_YSE_SET:
				Utils.setImage(context,iv_icon,R.drawable.listfile_yse_set_iv);
				break;
			default:
				Utils.setImage(context,iv_icon,R.drawable.listfile_file);
				break;
		}
	}
}
